﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "OwnerPtr.h"
#include "CommandBase.h"
#include "GraphicsNodeId.h"

/// 本文件是界面实现的辅助类，不包含GDMP API的演示内容

namespace gcmp
{
    class IGraphicsNode;
}

namespace Sample
{
    class IFieldProxy;
    typedef std::vector<gcmp::OwnerPtr<IFieldProxy>> ParametersArray;
    typedef std::map<std::wstring, ParametersArray> ParametersArrayDict;

    // get/set成员变量的类型
    enum class FieldType
    {
        String,
        Bool,
        Int,
        Double,
        Enum,
        List
    };

    // 对应C++对象的类似Get/Set这类简单成员函数，可以在GRep浏览器中显示和修改成员变量
    class IFieldProxy
    {
    public:
        typedef std::function<void(bool flag)> SetBoolFieldFunc;
        typedef std::function<bool(bool flag)> SetBoolFieldFuncBool;
        typedef std::function<void(std::wstring strValue)> SetTextOrEnumFieldFunc;
        typedef std::function<bool(const std::wstring& strValue)> SetTextConstRefFieldFuncBool;
        typedef std::function<void(int intValue)> SetIntFieldFunc;
        typedef std::function<void(double doubleValue)> SetDoubleFieldFunc;
        typedef std::function<bool(double doubleValue)> SetDoubleFieldFuncBool;

        template<typename T>
        static SetBoolFieldFunc CreateSetBoolFieldFunc(void (T::*func)(bool flag), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }

        template<typename T>
        static SetBoolFieldFuncBool CreateSetBoolFieldFuncBool(bool (T::*func)(bool flag), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }

        template<typename T>
        static SetIntFieldFunc CreateSetIntFieldFunc(void (T::*func)(int intValue), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }

        template<typename T>
        static SetDoubleFieldFunc CreateSetDoubleFieldFunc(void (T::*func)(double doubleValue), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }

        template<typename T>
        static SetDoubleFieldFuncBool CreateSetDoubleFieldFuncBool(bool (T::*func)(double doubleValue), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }

        template<typename T>
        static SetTextOrEnumFieldFunc CreateSetTextFieldFunc(void (T::*func)(std::wstring strValue), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }

        template<typename T>
        static SetTextConstRefFieldFuncBool CreateSetTextConstRefFieldFuncBool(bool (T::*func)(const std::wstring& strValue), T* object)
        {
            if (object)
            {
                return std::bind(func, object, std::placeholders::_1);
            }
            return nullptr;
        }
    public:
        IFieldProxy(std::wstring fieldName, std::wstring fieldValue)
            : m_fieldName(fieldName), m_fieldValue(fieldValue), m_isReadOnly(true), m_isUpdated(false)
        {
            m_fieldType = FieldType::String;
        }

        IFieldProxy(std::wstring fieldName, std::wstring fieldValue, std::vector<std::wstring> enumList)
            : m_fieldName(fieldName), m_fieldValue(fieldValue), m_isReadOnly(true), m_isUpdated(false)
        {
            m_fieldType = FieldType::Enum;
            m_enumList = enumList;
        }

        IFieldProxy(std::wstring fieldName, bool boolValue)
            : m_fieldName(fieldName), m_boolValue(boolValue), m_isReadOnly(true), m_isUpdated(false)
        {
            m_fieldType = FieldType::Bool;
        }

        IFieldProxy(std::wstring fieldName, int intValue)
            : m_fieldName(fieldName), m_intValue(intValue), m_isReadOnly(true), m_isUpdated(false)
        {
            m_fieldType = FieldType::Int;
        }

        IFieldProxy(std::wstring fieldName, double doubleValue)
            : m_fieldName(fieldName), m_doubleValue(doubleValue), m_isReadOnly(true), m_isUpdated(false)
        {
            m_fieldType = FieldType::Double;
        }
    public:
        /// 暂时为空，将来用于在GRep浏览器中显示提示信息
        std::wstring GetDescription() const { return L""; }
        /// GRep浏览器中左侧列的成员函数名称
        std::wstring GetFieldName() const { return m_fieldName; }
        
        /// GRep浏览器中右侧列的成员函数：
        /// 文本值
        std::wstring GetFieldValue();
        /// 布尔值
        bool GetValueAsBool() const { return m_boolValue; }
        /// 整数值
        int GetValueAsInt() const { return m_intValue; }
        /// 下拉菜单的值
        std::vector<std::wstring> GetEnumList() const {return m_enumList;}

        /// 判断唯一性和搜索用
        virtual int GetUniqueId() const
        {
            std::hash<std::wstring> hashFunc;
            return (int)hashFunc(m_fieldName);
        }
        FieldType GetFieldType() { return m_fieldType; }

        /// 更新数值到IFieldProxy，后面需要用类似SetSetBoolFieldFunc更新到成员函数
        void UpdateValue(bool newValue);
        void UpdateValue(std::wstring newValue);
        void UpdateValue(int newValue);
        void UpdateValue(double newValue);
    public:
        bool IsReadOnly() { return m_isReadOnly; }
        // 设置成员函数的函数指针
        void SetSetBoolFieldFunc(SetBoolFieldFunc setFieldFunc);
        void SetSetBoolFieldFuncBool(SetBoolFieldFuncBool setFieldFuncBool);
        void SetSetEnumFieldFunc(SetTextOrEnumFieldFunc setEnumFieldFunc);
        void SetSetTextConstRefFieldFuncBool(SetTextConstRefFieldFuncBool);
        void SetSetIntFieldFunc(SetIntFieldFunc setIntFieldFunc);
        void SetSetDoubleFieldFunc(SetDoubleFieldFunc setDoubleFieldFunc);
        void SetSetDoubleFieldFuncBool(SetDoubleFieldFuncBool setDoubleFieldFuncBool);
        // 运行设置成员函数
        void RunSetFieldFunc();
    private:
        std::wstring m_fieldName;
        std::wstring m_fieldValue;
        bool m_boolValue;
        int m_intValue;
        double m_doubleValue;
        std::vector<std::wstring> m_enumList;
        FieldType m_fieldType;

        bool m_isUpdated;
        bool m_isReadOnly;
        SetBoolFieldFunc m_setBoolFieldFunc;
        SetBoolFieldFuncBool m_setBoolFieldFuncBool;
        SetTextOrEnumFieldFunc m_setEnumFieldFunc;
        SetIntFieldFunc m_setIntFieldFunc;
        SetDoubleFieldFunc m_setDoubleFieldFunc;
        SetDoubleFieldFuncBool m_setDoubleFieldFuncBool;
        SetTextConstRefFieldFuncBool m_setTextConstRefFieldFuncBool;
    };
}

